﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAOTAgency;

namespace TravelAgency
{
    public partial class WebFormEditReservations : System.Web.UI.Page
    {
        private static int reservationId;

        protected void Page_Load(object sender, EventArgs e)
        {
            DivEdit.Visible = false;
            LabelNoResult.Visible = false;
            GridViewReservations.DataSource = DAO.GetAllReservations();
            GridViewReservations.DataBind();
        }

        private void ShowDiv()
        {
            DivEdit.Visible = true;
            Reservation res = DAO.GetAllReservations().Where(r => r.ReservationID.Equals(reservationId)).SingleOrDefault();
            TextBoxDiscount.Text = res.Discount.ToString();
            TextBoxPaid.Text = res.PaidPrice.ToString();
            if (res.TotalPrice == res.PaidPrice)
            {
                TextBoxPaid.Enabled = false;
                ReseravationStatus.Text = "confirmed";
            }
            else
            {
                TextBoxPaid.Enabled = true;
                if (res.PaidPrice == 0)
                {
                    ReseravationStatus.Text = "unconfirmed";
                }
                else
                {
                    ReseravationStatus.Text = "partially paid";
                }
            }
        }

        protected void ButtonFilter_Click(object sender, EventArgs e)
        {
            List<Reservation> reservations = DAO.ReservationFilter(ReferenceNumber.Text).ToList();
            if (reservations.Count > 0)
            {
                GridViewReservations.Visible = true;
                LabelNoResult.Visible = false;
                GridViewReservations.DataSource = reservations;
                GridViewReservations.DataBind();
            }
            else
            {
                GridViewReservations.Visible = false;
                LabelNoResult.Visible = true;
            }
        }

        protected void GridViewReservations_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] =
                   "this.style.background='#EEEEEE';this.style.cursor='hand'";
                e.Row.Attributes["onmouseout"] =
                    "this.style.background='white'";

                LinkButton buttonModify = e.Row.FindControl("ButtonModify") as LinkButton;
                buttonModify.CommandArgument = DataBinder.Eval(e.Row.DataItem, "ReservationID").ToString();                
            }
        }

        protected void ButtonModify_Click(object sender, EventArgs e)
        {
            // Retrieve control
            LinkButton linkButton = sender as LinkButton;
            reservationId = int.Parse(linkButton.CommandArgument);
            ShowDiv();
        }


        protected void ButtonShowAll_Click(object sender, EventArgs e)
        {
            GridViewReservations.Visible = true;
            LabelNoResult.Visible = false;
            GridViewReservations.DataSource = DAO.GetAllReservations();
            GridViewReservations.DataBind();
        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            DAO.ModifyReservation(reservationId, decimal.Parse(TextBoxPaid.Text), double.Parse(TextBoxDiscount.Text));
            GridViewReservations.DataSource = DAO.GetAllReservations();
            GridViewReservations.DataBind();
        }

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            DivEdit.Visible = false;
            DAO.Cancel();
        }
    }

    //http://forums.asp.net/t/1016380.aspx
    //http://nice-tutorials.blogspot.com/2009/08/how-to-fire-onclick-event-of-linkbutton.html
}